 

////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////

#include "CinematicManager.h"
#include "Cinematic.h"
#include "GameObjectManager.h"
#include "game.h"

CinematicManager* CinematicManager::m_cineManager = NULL;
#define MAX_NUM_CINEMATIC_OBJECTS 2
CinematicManager::CinematicManager()
{	
	m_cineManager = this;
	m_mustSkipCinematics = false;
	m_runningList.Reserve(MAX_NUM_CINEMATIC_OBJECTS);
	m_readyToRunFinalAction.Reserve(MAX_NUM_CINEMATIC_OBJECTS);

#if RUN_FINAL_CONFIGURATION_AS_LAST_ACTION
	m_justEndedList.Reserve (MAX_NUM_CINEMATIC_OBJECTS);
#endif

	//m_cinematicToReplay = 0;
	//m_oldCinematicToReplay = m_cinematicToReplay;
}

CinematicManager::~CinematicManager()
{
	m_runningList.Clear();

#if RUN_FINAL_CONFIGURATION_AS_LAST_ACTION
	m_justEndedList.Clear ();
#endif
}
//
//void CinematicManager::SaveData(CWriteFile* stream)
//{
//	/*if( !m_cinematicToReplay && m_oldCinematicToReplay )
//		stream->WriteS32(m_oldCinematicToReplay);
//	else
//		stream->WriteS32(m_cinematicToReplay);*/
//	stream->WriteU8 (m_mustSkipCinematics);
//
//	stream->WriteS32(m_runningList.GetCount());
//	for (int i=0;i<m_runningList.GetCount();i++)
//		stream->WriteGameObjPtr(m_runningList[i]);
//
//	stream->WriteS32(m_readyToRunFinalAction.GetCount());
//	for (int i=0;i<m_readyToRunFinalAction.GetCount();i++)
//		stream->WriteGameObjPtr(m_readyToRunFinalAction[i]);
//	
//}
//
//void CinematicManager::LoadData(CReadFile* stream)
//{
//	/*m_cinematicToReplay = stream->ReadS32();*/
//	m_mustSkipCinematics = stream->ReadU8();
//
//	m_runningList.Clear();
//	int c = stream->ReadS32();
//	for (int i=0;i<c;i++)
//		m_runningList.AddObject(stream->ReadGameObjPtr());
//
//
//	m_readyToRunFinalAction.Clear();
//	c = stream->ReadS32();
//	for (int i=0;i<c;i++)
//		m_readyToRunFinalAction.AddObject(stream->ReadGameObjPtr());
//}

void CinematicManager::Init()
{
	m_runningList.Clear();
	m_readyToRunFinalAction.Clear();
#if RUN_FINAL_CONFIGURATION_AS_LAST_ACTION
	m_justEndedList.Clear ();
#endif
}

//void CinematicManager::AddCinematic(int cinematicID)
//{
//	GameObjectListInterface* objs;
//	
//	objs = &GetGameObjectManager()->GetCinematicObjects();
//	
//
//	Cinematic* c = (Cinematic*)objs->FindObject(cinematicID);
//	if (c && c->IsEnabled() )
//		AddCinematic(c);
//}

void CinematicManager::CancelCurrentCinematics()
{
	m_runningList.Clear();

	//Game::Instance()->GetWorld().m_pMessageManager->ClearTrack(0);
}

void CinematicManager::AddCinematic(Cinematic* cinematic)
{
	if( !cinematic->IsEnabled() )
	{
		DEBUG_OUT("CinematicManager::AddCinematic %d Failed!!!!! cinematic disabled\n" , cinematic->GetID());
		return;
	}

		int fObj = m_runningList.FindObject( cinematic );
	if( fObj >= 0 )
		return;

	if (cinematic->initCinematic())
	{
		m_runningList.AddObject(cinematic);
	}
	else
	{
		DEBUG_OUT("CCinematicManager::AddCinematic %d failed on Init \n" , cinematic->GetID() );
	}	
}

void CinematicManager::RemoveCinematic(Cinematic* cinematic)
{
	m_runningList.RemoveObject(cinematic);		
}

void CinematicManager::SkipCurrentCinematics ()
{
	m_mustSkipCinematics = true;
}

bool CinematicManager::IsActive( Cinematic* cinematic )
{
	return ( m_runningList.FindObject( cinematic ) >= 0 );
}

void CinematicManager::update(float timeStep)
{
	if(getGame()->IsTimeFrezze())
		return;
	int numObjs = 0;

#if RUN_FINAL_CONFIGURATION_AS_LAST_ACTION
	numObjs = m_justEndedList.GetCount ();

	if (numObjs)
	{
		for (int i = 0; i < numObjs;i ++)
		{
			Cinematic* cine = (Cinematic*)m_justEndedList[i];
			cine->parseFinalAction ();
		}

		m_justEndedList.Clear ();
	}
#endif
	
	numObjs = m_runningList.GetCount();

	if (numObjs != 0)
	{
		for( int i = numObjs - 1; i >= 0; i-- )
		{
			Cinematic* cine = (Cinematic*)m_runningList[i];

			if (m_mustSkipCinematics)
			{								
				m_readyToRunFinalAction.AddObject(cine);
				m_runningList.RemoveObject(cine);
			}
			else
			{	
				cine->updateCinematic (timeStep);
				E_CINEMATIC_CODES r = cine->GetState();
				
				if ( r != E_CINEMATIC_IN_PROGRESS )
				{
				#if RUN_FINAL_CONFIGURATION_AS_LAST_ACTION
					m_justEndedList.AddObject (cine);
				#endif
					//cine->reset();
					m_runningList.RemoveObject(cine);
				}
			}
		}
	}
	/*else
		if( m_cinematicToReplay )
		{
			AddCinematic( m_cinematicToReplay );
			m_oldCinematicToReplay = m_cinematicToReplay;
			m_cinematicToReplay = 0;
		}*/

	if (m_mustSkipCinematics)
	{
		numObjs = m_readyToRunFinalAction.GetCount();

		for(int i = 0;i < numObjs; i ++)
		{
			Cinematic* cine = (Cinematic*)m_readyToRunFinalAction[i];
			cine->parseFinalAction ();
		}

		m_readyToRunFinalAction.Clear();
	}

	if (m_mustSkipCinematics)
	{
		m_mustSkipCinematics = false;
	}
}

bool CinematicManager::IsRunning()
{
	return (m_runningList.GetCount() > 0);
}